﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScrollCircle : ScrollRect
{
    float m_radius = 0f;
    protected override void Start()
    {
        base.Start();
        m_radius = transform.GetComponent<RectTransform>().sizeDelta.x * 0.5f;
        content = transform.Find("touch").transform.GetComponent<RectTransform>();
    }

    public override void OnDrag(UnityEngine.EventSystems.PointerEventData eventData)
    {
        base.OnDrag(eventData);
        Vector2 centerPos = this.content.anchoredPosition;
        if (centerPos.magnitude > m_radius)
        {
            centerPos = centerPos.normalized * m_radius;
            SetContentAnchoredPosition(centerPos);
        }
    }
}

//测试坐标变换
//RectTransformUtility.ScreenPointToLocalPointInRectangle 屏幕坐标转为对应recttransform的世界坐标
//public class ScrollCircle : MonoBehaviour
//{
//    float m_radius = 0f;
//    RectTransform touch;
//    Camera ui;
//    void Start() {
//        m_radius = transform.GetComponent<RectTransform>().sizeDelta.x * 0.5f;
//        touch = transform.FindChild("touch").transform.GetComponent<RectTransform>();
//        ui=GameObject.Find("UICamera").GetComponent<Camera>();
//    }
//    void Update() {
//        if (Input.GetMouseButtonDown(0)) {
//            //从鼠标坐标点转成世界坐标点的时候，新的三维向量Z轴的赋值必须不能是0.0f，因为，屏幕坐标点的Z轴，
//            //其实是相对于当前摄像机的，如果是0，只能转一次世界坐标系，剩下的不管点哪里都是一个值，所以不能把Z值设为和摄像机的Z坐标重合
//            var pos = transform.parent.InverseTransformPoint(ui.ScreenToWorldPoint(Input.mousePosition));
//            Vector3 screen = transform.GetComponent<RectTransform>().anchoredPosition;// ui.WorldToScreenPoint(transform.position);
//            Debug.Log(pos);
//            Debug.Log(screen);
//            if (Vector3.Distance(pos, screen) > m_radius)
//            {
//                pos = pos.normalized * m_radius;
//            }
//            else {
//                pos = new Vector3(touch.anchoredPosition.x,touch.anchoredPosition.y,0) + pos - screen;
//            }
//            //Vector3 world = ui.ScreenToWorldPoint(pos) ;
//            //Debug.Log(world);
//            //touch.position = new Vector3(world.x, world.y, 0);
//            //touch.localPosition = new Vector3(world.x, world.y, 0);
//            touch.anchoredPosition =pos;
//        }
//    }
//}
